package com.okaixz.client.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.okaixz.client.mapper.GoodsMapper;
import com.okaixz.client.model.domain.Goods;
import com.okaixz.client.service.IGoodsService;
import com.okaixz.common.client.exception.BusinessException;
import com.okaixz.common.client.exception.CommonErrorCode;
import com.okaixz.common.constant.CommonConstants;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cdz
 * @since 2024-10-17
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Override
    public List<Goods> getListByType(Integer type) {
        if (type == null) {
            return list();
        }
        List<Goods> list = lambdaQuery()
                .eq(Goods::getType, type)
                .eq(Goods::getStatus, CommonConstants.GOODS_STATUS_ENABLE)
                .list();
        return list;
    }

    @Override
    public List<Goods> getListByClientName(String clientName) {
        if (clientName == null) {
            throw new BusinessException(CommonErrorCode.CLIENT_NOT_EXIST);
        }
        List<Goods> list = lambdaQuery()
                .eq(Goods::getClientName, clientName)
                .eq(Goods::getStatus, CommonConstants.GOODS_STATUS_ENABLE)
                .eq(Goods::getType, CommonConstants.GOODS_TYPE_VIP)
                .orderByDesc(Goods::getOrderNum)
                .list();
        return list;
    }

    @Override
    public List<Goods> getVipList() {
        List<Goods> result = new ArrayList<>();
        List<Goods> list = lambdaQuery()
                .eq(Goods::getType, 1)
                .eq(Goods::getStatus, CommonConstants.GOODS_STATUS_ENABLE)
                .list();
        List<Goods> list2 = lambdaQuery()
                .eq(Goods::getType, 2)
                .eq(Goods::getStatus, CommonConstants.GOODS_STATUS_ENABLE)
                .list();
        if (list2 != null) {
            result.addAll(list2);
        }
        if (list != null) {
            result.addAll(list);
        }
        return result;
    }

    @Override
    public List<Goods> getTokensList() {
        List<Goods> result = new ArrayList<>();
        List<Goods> list = lambdaQuery()
                .eq(Goods::getType, 0)
                .eq(Goods::getStatus, CommonConstants.GOODS_STATUS_ENABLE)
                .list();
        List<Goods> list2 = lambdaQuery()
                .eq(Goods::getType, 2)
                .eq(Goods::getStatus, CommonConstants.GOODS_STATUS_ENABLE)
                .list();
        if (list2 != null) {
            result.addAll(list2);
        }
        if (list != null) {
            result.addAll(list);
        }
        return result;
    }

    @Override
    public Goods getGoodsById(Integer gid,String clientName) {
        Goods goods = lambdaQuery()
                .eq(Goods::getStatus, CommonConstants.GOODS_STATUS_ENABLE)
                .eq(Goods::getId, gid)
                .eq(Goods::getClientName,clientName)
                .one();
        return goods;
    }
}
